package com.concurrent.frame.processor.task;

import com.concurrent.frame.core.ResourceRegisterPool;
import com.concurrent.frame.pojo.JobInfo;
import com.concurrent.frame.pojo.TaskResult;
import com.concurrent.frame.pojo.TaskResultType;
import com.concurrent.frame.util.Assert;

public abstract class AbsTaskSingleProcessor<T,R> implements ITaskProcessor<T,R>{
    protected JobInfo<R> jobInfo;
    public AbsTaskSingleProcessor(JobInfo<R> jobInfo) {
        this.jobInfo = jobInfo;
    }
    @Override
    public void process(final T o) {
        ResourceRegisterPool.getTaskExecutor().submit(new Runnable() {
            @Override
            public void run() {
                TaskResult<R> result = null;
                try {
                    result = taskExecute(o);
                    result = Assert.isTaskResult(result);
                }catch (Exception e){
                    e.printStackTrace();
                    result = new TaskResult<R>(TaskResultType.Exception,null,e.getMessage());
                }finally {
                    jobInfo.addTaskResult(result);
                }
            }
        });
    }
    public abstract TaskResult<R> taskExecute(T t);
}
